package net.zyj.student.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.logging.SimpleFormatter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.list.SetUniqueList;

import net.sf.json.JSONObject;
import net.zyj.student.mapper.StudentMapper;
import net.zyj.student.pojo.Student;
import net.zyj.student.pojo.Teacher;

public class StudentAction extends HttpServlet {

	private StudentMapper studentMapper = new StudentMapper();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}
	/**
	 * 辅导员可以管理的学生列表
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void list(HttpServletRequest request,HttpServletResponse response)throws IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		//前台传来的分页信息
		int page=Integer.parseInt(request.getParameter("page"));
		int rows=Integer.parseInt(request.getParameter("rows"));
		Teacher teacher=(Teacher)request.getSession().getAttribute("teacher");
		//设置sql语句所需要的参数
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("teacherid", teacher.getId());
		map.put("start", (page-1)*rows);
		map.put("count", rows);
		if(request.getParameter("queryName")!=null){
			map.put("queryName", "%"+request.getParameter("queryName")+"%");
		}
		List<Student> list=studentMapper.selectListByteacherId(map);
		int total=studentMapper.countByteacherId(map);
		Map<String, Object> responseMap=new HashMap<String, Object>();
		responseMap.put("total", total);
		responseMap.put("rows", list);
		JSONObject json=JSONObject.fromObject(responseMap);
		out.print(json);
		out.close();
	}
	/**
	 * 新增学生
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void add(HttpServletRequest request,HttpServletResponse response)throws IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		Map<String, Object> map=new HashMap<String, Object>();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//Date date = sdf.parse("");
		try {
			Student student=new Student();
			student.setName(request.getParameter("name"));
			student.setClassId(Integer.parseInt(request.getParameter("classId")));
			student.setStatus(request.getParameter("status"));
			student.setSex(request.getParameter("sex"));
			student.setDomicile(request.getParameter("domicile"));
			student.setDomicileType(request.getParameter("domicileType"));
			student.setAddress(request.getParameter("address"));
			student.setPostalCode(Integer.parseInt(request.getParameter("postalCode")));
			student.setPhone(request.getParameter("phone"));
			student.setqQ(request.getParameter("qQ"));
			student.setPostBox(request.getParameter("postBox"));
			student.setEduBackground(request.getParameter("eduBackground"));
			student.setBirthDate(sdf.parse(request.getParameter("birthDate")));
			student.setNation(request.getParameter("nation"));
			student.setPuliticStatus(request.getParameter("puliticStatus"));
			student.setCard(request.getParameter("card"));
			student.setAnnex(request.getParameter("annex"));
			student.setType(request.getParameter("type"));
			student.setRole(request.getParameter("role"));
			student.setTerm(request.getParameter("Term"));
			student.setJob(request.getParameter("job"));
			studentMapper.save(student);
			map.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", "新增学生失败！");
		}
		JSONObject json=JSONObject.fromObject(map);
		out.print(json);
		out.close();
	}
	/**
	 * 根据id查找学生
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void getByid(HttpServletRequest request,HttpServletResponse response) throws IOException{
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		int id=Integer.parseInt(request.getParameter("id"));
		Student student=studentMapper.selectById(id);
		JSONObject json=JSONObject.fromObject(student);
		out.print(json);
		out.close();
	}
	private void edit(HttpServletRequest request,HttpServletResponse response)throws IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();		
		Map<String,Object> map = new HashMap<String, Object>();
		try {
			Student student=new Student();
			student.setId(Integer.parseInt(request.getParameter("id")));
			
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
